package com.pf.java;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author pf
 * @data 2021/11/1-10:33
 */
public class map {

    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("1", 70);
        map.put("2", 80);
        map.put("3", 90);
        System.out.println(map);

        // value 可以重复， key 不能重复会导致覆盖
        map.put("4",80);
        map.put("2",85);
        System.out.println(map);

        map.remove("3");
        System.out.println(map);

        System.out.println(map.get("2"));
        System.out.println(map.containsKey("2"));
        System.out.println(map.containsValue(85));

        // 遍历，数据对Entry类型
        // entrySet()
        for (Object entry : map.entrySet()) {
            Map.Entry e = (Map.Entry) entry;
            System.out.println(e.getKey() + " " + e.getValue());
        }

        // keySet()
        for (Object key : map.keySet()) {
            System.out.println(key + " " + map.get(key));
        }

        Set set = map.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            System.out.println(key + ": " + map.get(key));
        }

        // values()
        for (Object value : map.values()) {
            System.out.println(value);
        }

    }
}
